require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIBattleFail = {};
HelperFunc.SetUIEnv(UIBattleFail)
local this = UIBattleFail;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.btnBgClose = nil;
this.btnHeroRank = nil;
this.btnEquipRank = nil;
this.btnEquipRefine = nil;
this.btnFormation = nil;
this.btnManual = nil;
this.spineAniPos = nil;
this.btnData = nil;
this.drawWrapper = nil;
this.spineAni = nil;
this.spineAniTimer = nil;
this.typeController = nil;

this.finishFuction = nil;
this.resultData = nil;

this.rank = nil
this.score = nil
--秘境
this.scoreItem = nil;--评分组件
this.itemScoreList = nil;--奖励列表
this.rewards = {};--奖励数据
this.rankBattleScore = nil
this.rankBattleAddScore = nil
this.coinTip = nil

function UIBattleFail.Init(func, result)
    --todo 应该小写 暂时先大写
    finishFuction = func;
    resultData = result;
    local name, packageName = getUIName(PanelNames.UIBattleFail);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIBattleFail.onCreate(obj)
    AudioManager.Play(7001)
    --战斗失败界面背景音乐
    AudioManager.SetSwitch("battle_steps", "end")
    AudioManager.SetSwitch("fight_result", "lose")
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        btnBgClose = obj:GetChild("BgClose");
        btnHeroRank = obj:GetChild("HeroRankBtn");
        btnEquipRank = obj:GetChild("EquipRankBtn");
        btnEquipRefine = obj:GetChild("EquipRefineBtn");
        btnFormation = obj:GetChild("FormationBtn");
        btnManual = obj:GetChild("ManualBtn");
        this.HeroLevelBtn = obj:GetChild("HeroLevelBtn");
        this.Text_guildtips = obj:GetChild("Text_guildtips");
        btnData = obj:GetChild("DataBtn");
        spineAniPos = obj:GetChild("spineAniPos");
        this.arenaLayer = obj:GetChild("arena");
        this.arenaPoint = this.arenaLayer:GetChild("point");
        this.arenaChange = this.arenaLayer:GetChild("pointChange")

        -- this.arenaLayer2 = obj:GetChild("arena2")
        -- this.arenaIfNoRewardsCtrl = this.arenaLayer2:GetController("ifNoRewards")
        -- this.arenaServerIfCtrl = this.arenaLayer2:GetController("ServerIf")

        this.rankBattleScore = obj:GetChild("Text_RankBattlePointNum")
        this.rankBattleAddScore = obj:GetChild("Text_RankBattlePointChange")
        this.rankBattleItem = obj:GetChild("Btn_RankBattleCoin")
        this.coinTip =  obj:GetChild("Text_RankBattleCoinLimit")
        typeController = obj:GetController("type");
        this.shibai = obj:GetChild("shibai").wrapTarget;

        this.rank = obj:GetChild("n112")
        this.score = obj:GetChild("n113")

        itemScoreList = obj:GetChild("ItemScrollView");
        scoreItem = obj:GetChild("n126");
        
        logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBattleFail,Config.FuncOpenType.HeroWeapon,btnEquipRank.name,btnEquipRank)
        --spineAni = obj:GetChild("ui_battle_lose"):GetComponent("SkeletonGraphic");
        this.addEvent();
    end
    Util.PlayAction(this.shibai, "start", false);
    if this.shibaiTime then
        utimer.remove(this.shibaiTime);
    end
    this.shibaiTime = utimer.timeCall(function ()
        Util.PlayAction(this.shibai, "idle", true);
        this.shibaiTime = nil;
    end, 1500);
    this.refresh();

end

function UIBattleFail.addEvent()
    btnBgClose.onClick:Add(this.onClickBGClose);
    btnHeroRank.onClick:Add(this.onClickHeroRank);
    btnEquipRank.onClick:Add(this.onClickEquipRank);
    btnEquipRefine.onClick:Add(this.onClickEquipRefine);
    btnFormation.onClick:Add(this.onClickFormation);
    btnManual.onClick:Add(this.onClickManual);
    btnData.onClick:Add(this.onClickData);
    this.HeroLevelBtn.onClick:Add(this.onClickHeroLevel);

end

function UIBattleFail.removeEvent()
    btnBgClose.onClick:Clear();
    btnHeroRank.onClick:Clear();
    btnEquipRank.onClick:Clear();
    btnEquipRefine.onClick:Clear();
    btnFormation.onClick:Clear();
    btnManual.onClick:Clear();
    btnData.onClick:Clear();
    this.HeroLevelBtn.onClick:Clear();

end

function UIBattleFail.LocalListenEvent()

end

function UIBattleFail.LocalCloseEvent()

end

function UIBattleFail.OnDestroy()
    this.removeEvent();
    if (spineAni ~= nil) then
        destroy(spineAni);
    end
    spineAni = nil
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIBattleFail)
end

function UIBattleFail.OnEnable()
    this.LocalListenEvent();
    logicMgr.GuideMgr.Pause(GuideMgr.PauseType.battleResultPause)
end

function UIBattleFail.OnDisable()
    this.LocalCloseEvent();
    logicMgr.BattlePlaybackManager.ClearReceiveResult()
    logicMgr.GuideMgr.Resume(GuideMgr.PauseType.battleResultPause)
end

function UIBattleFail.refresh()
    this.teamNum = logicMgr.MapManager.getStageNeedTeamNum();
    btnData.visible = not (FightManager.isRePlay == true)
    if FightManager.GetDungeonWinRePlay() then
        btnData.visible = true;
    end
    local record = logicMgr.BattlePlaybackManager.getBattleRecord()
    if not record or record.noFight == 1 then
        btnData.visible = false
    end
    --spineAni.gameObject:SetActive(true);
    --spineAni.AnimationState:SetAnimation(0,"lose_start",false);
    --spineAni:Update(0);
    Util.AddAction(spineAni, "lose_start", false, 0.5);

    if spineAniTimer == nil then
        spineAniTimer = Timer.New(this.switchAni, 0.5, 1, false);
    else
        spineAniTimer:Reset(this.switchAni, 0.5, 1, false);
    end
    spineAniTimer:Start();
    btnManual.visible = false;   --代码保护
    this.nextId = nil;
    local fightType = logicMgr.BattlePlaybackManager.GetFightType()
    if fightType == EnumConst.FightTypeConst.DUNGEON then
        typeController.selectedIndex = 0;
        this.nextId = logicMgr.MapManager.getNextStage();
        if logicMgr.FuncOpenManager.isOpen(208) then
            btnManual.visible = true;
        end
    elseif fightType == EnumConst.FightTypeConst.TOWER then
        typeController.selectedIndex = 0;
        this.nextId = logicMgr.TowerManager.returnTowerInfo(1)["layerId"]
    elseif fightType == EnumConst.FightTypeConst.ARENA then
        typeController:SetSelectedIndex(1)
        local formation_type = logicMgr.BattlePlaybackManager.GetFormationType()
        local lastData = nil
        local nowData = nil
        if formation_type and formation_type == EnumConst.FormationType.CrossArenaAtk then
            lastData = logicMgr.ArenaManager.getLastCrossBaseInfo()
            nowData = logicMgr.ArenaManager.getCrossBaseInfo()
        elseif formation_type and formation_type == EnumConst.FormationType.ArenaAtk then
            lastData = logicMgr.ArenaManager.getLastBaseInfo()
            nowData = logicMgr.ArenaManager.getBaseInfo()
        elseif formation_type and formation_type == EnumConst.FormationType.TeamCrossArenaAtk then
            typeController:SetSelectedIndex(5)
            local arenaLayer3 = gameObject:GetChild("arena2")
            local icon_my = arenaLayer3:GetChild("IconMy")
            local txt_my_level = arenaLayer3:GetChild("levelMy")
            local icon_enemy = arenaLayer3:GetChild("IconEnemy")
            local txt_enemy_level = arenaLayer3:GetChild("levelEnemy")
            local txt_num = arenaLayer3:GetChild("num")
            local win_count = FightManager.cross_team_battle_data and FightManager.cross_team_battle_data["win_count"] or 0
            txt_num.text = string.format("%s:%s", win_count, 3-win_count) 
            local glist = gameObject:GetChild("list")
            glist.itemRenderer = this.ArenaItemRender
            glist.numItems = 3

            local attacker_score = FightManager.cross_team_battle_data and FightManager.cross_team_battle_data["attacker_score"]
            local cfg_grading, cfg_arena_matching_item = ArenaManager.getAthleticGradingConfigByRank(attacker_score) 
            txt_my_level.text = getLanguage(cfg_arena_matching_item.f_RankName)
            icon_my.url = EnumConst.AtlasPath.ICONAtlas .. cfg_grading.f_Icon

            local my_img_level = arenaLayer3:GetChild("MyEffect_Dwsg")
            local my_level = cfg_grading.f_RankId
            my_img_level.animationName = "idle" .. my_level
            my_img_level.skinName = my_level
            my_img_level.loop = true 

            local defener_score = FightManager.cross_team_battle_data and FightManager.cross_team_battle_data["defener_score"]
            local cfg_grading1, cfg_matching1 = ArenaManager.getAthleticGradingConfigByRank(defener_score) 
            icon_enemy.url = EnumConst.AtlasPath.ICONAtlas .. cfg_grading1.f_Icon
            txt_enemy_level.text = getLanguage(cfg_matching1.f_RankName)

            local enemy_img_level = arenaLayer3:GetChild("EnemyEffect_Dwsg") 
            local enemy_level = cfg_grading1.f_RankId
            enemy_img_level.animationName = "idle" .. enemy_level
            enemy_img_level.skinName = enemy_level
            enemy_img_level.loop = true
        end
        if lastData ~= nil and nowData ~= nil and next(nowData) ~= nil and next(lastData) ~= nil then
            this.arenaPoint.text = lastData.score or 0;
            this.arenaChange.text = nowData.score - lastData.score;
        end
    elseif fightType == EnumConst.FightTypeConst.INSTANCE then
        rewards = logicMgr.MythicDungeonsProManager.getItemInfos();

        itemScoreList.itemRenderer = this.rendererReward;
        itemScoreList.numItems = #rewards;

        scoreItem:GetController("rank").selectedIndex = 4;
        logicMgr.MythicDungeonsManager.SetLayerLevel(5)
        typeController.selectedIndex = 2;
    elseif fightType == EnumConst.FightTypeConst.FriendAttack then
        typeController.selectedIndex = 3;
        local result = logicMgr.FriendBattleManager.GetFightResult()
        local myInfo = logicMgr.FriendBattleManager.GetFriendChallengeList()
        local rank = tonumber(tostring(result.rank)) or 0
        local lastRank = tonumber(tostring(myInfo.rank)) or 0
        local score = tonumber(tostring(result.score)) or 0
        local lastScore = tonumber(tostring(myInfo.score)) or 0
        local rnum = math.abs(rank - lastRank)
        local rstr = ""
        if rnum ~= 0 then
            rstr = "(".."-"..rnum..")"
        end
        this.rank.text = rank..rstr
        local num = math.abs(score - lastScore)
        local str = ""
        if num ~= 0 then
            str = "(".."-"..num..")"
        end
        this.score.text = score..str
    elseif fightType == EnumConst.FightTypeConst.RankBattle then
        typeController.selectedIndex = 4;
        local config = logicMgr.RankBattleManager.GetResult()
        local info = logicMgr.RankBattleManager.GetMyInfo()
        local scoreNum = tonumber(tostring(info.score))
        if scoreNum ==0 then
            scoreNum = 1200
        end
        this.rankBattleScore.text = tostring(scoreNum - config.score)
        this.rankBattleAddScore.text = "-"..tostring(-config.score)
        logicMgr.CostManager.RefreshItem(this.rankBattleItem, {CurrencyEnum.GOLD, config.coin});
        local hasNum = tonumber(tostring(info.weekCoin))
        local maxNum = G.dataTable["t_global"]["208"].f_int_value
        this.coinTip.text = string.format(getLanguage("RankBattleResultTips"),hasNum,maxNum)
    elseif fightType == EnumConst.FightTypeConst.GuildBattlePlus then
        typeController:SetSelectedIndex(6)
        this.Text_guildtips.text = getLanguage("GuildBattlePlusTips025")
        local Com_GuildBattlePlus = gameObject:GetChild("Com_GuildBattlePlus")
        local txt_num = Com_GuildBattlePlus:GetChild("num")
        this.guideBattleList = logicMgr.GuildBattlePlusManager.getAttackInfo();
        txt_num.text = string.format("%s:%s", logicMgr.GuildBattlePlusManager.getAttackScore())
        local glist = gameObject:GetChild("list")
        glist.itemRenderer = this.GuildBattlePlusItemRender
        glist.numItems = #this.guideBattleList
    else
        typeController.selectedIndex = 0;
        btnManual.visible = false;
    end
end

function UIBattleFail.ArenaItemRender(index, obj)
    this.UpdateItemRender(index, obj, FightManager.cross_team_battle_data[index+1]);
end
function UIBattleFail.GuildBattlePlusItemRender(index, obj)
    this.UpdateItemRender(index, obj,  this.guideBattleList[index+1]);
end
function UIBattleFail.UpdateItemRender(index, obj, data)
    if data ~= nil then
        local ctrl_win = obj:GetController("ifWin")
        local txt_my_name = obj:GetChild("nameMy")
        local txt_enemy_name = obj:GetChild("nameEnemy")
        local head_my = obj:GetChild("headMy")
        local head_enemy = obj:GetChild("headEnemy")
        local my_hero_list = obj:GetChild("heroMy")
        local enemy_hero_list = obj:GetChild("heroEnemy")
        local txt_num = obj:GetChild("num")
        local btn_play = obj:GetChild("n5")
        btn_play.onClick:Clear()

        local num_str = "第一场"
        if index == 1 then
            num_str = "第二场"
        elseif index == 2 then
            num_str = "第三场"
        end
        txt_num.text= num_str
        if data.win == 1 then
            ctrl_win:SetSelectedIndex(0)
        else
            ctrl_win:SetSelectedIndex(1)
        end
        btn_play.onClick:Add(function()
            BattlePlaybackManager.ClearMultiFightData()
            BattlePlaybackManager.AddMultiFightData( data.fightId, data.win, logicMgr.BattlePlaybackManager.GetFightType(), data, nil, -1, -1)
            BattlePlaybackManager.GotoNextFight(true)
        end)

        local attacker = data.attacker
        local defender = data.defender
        txt_my_name.text = attacker.playerName or "--" 
        txt_enemy_name.text = defender.playerName or "--" 

        head_my:GetChild("head"):GetChild("HeadIcon").url =logicMgr.SkinManager.GetHeroIconBySkinId(attacker.pic)
        head_my:GetChild("frame").url = EnumConst.AtlasPath.ICONAtlas .. attacker.picFrame

        head_enemy:GetChild("head"):GetChild("HeadIcon").url = logicMgr.SkinManager.GetHeroIconBySkinId(defender.pic)
        head_enemy:GetChild("frame").url = EnumConst.AtlasPath.ICONAtlas .. defender.picFrame

        my_hero_list.itemRenderer = this.renderHeroItem
        enemy_hero_list.itemRenderer = this.renderHeroItem2
        this.my_hero_list_data = attacker.members or {}
        my_hero_list.numItems = #this.my_hero_list_data
        this.enemy_hero_list_data = defender.members or {}
        enemy_hero_list.numItems = #this.enemy_hero_list_data
    end
end

function UIBattleFail.renderHeroItem(index, obj)
    local data = this.my_hero_list_data[index+1]
    this.showItemInfo( obj, data )
end
function UIBattleFail.renderHeroItem2(index, obj) 
    local data = this.enemy_hero_list_data[index+1]
    this.showItemInfo( obj, data )
end
function UIBattleFail.showItemInfo( obj, item_info ) 
    local quality = obj:GetChild("quality")
    local qualityBG = obj:GetChild("qualityBG")
    local icon = obj:GetChild("icon") 
    local lv = obj:GetChild("level")
    local lvbg = obj:GetChild("n29")  
    local choose = obj:GetChild("maskChoose")
    local unActive = obj:GetChild("unActive") 
    local circleCtrl = obj:GetController("Circle")
    local comIcon = obj:GetChild("Com_icon") 
    local levelShowCtrl = obj:GetController("isShowLevel")--0显示等级以及底框 1不显示 
    quality.visible = false
    qualityBG.visible = false
    choose.visible = false
    unActive.visible = false 
    icon.visible = false 
    if item_info then  
        if item_info.star ~= nil then
            item_info.quality = logicMgr.HeroManager.starToQuality(item_info["star"]);
        end
        local hero_item = G.dataTable["t_hero"][tostring(item_info.partnerModelId)]
        if levelShowCtrl ~= nil then
            if item_info.level ~= nil and item_info.level > 0 then 
                levelShowCtrl:SetSelectedIndex(0) 
                lv.visible = true
                lvbg.visible = true
                local level, lvType = logicMgr.HeroManager.getHeroShowLevelByPartnerId(item_info.partnerId)
                lv.text = string.TextColor(string.format(getLanguage("Level"), level), logicMgr.HeroManager.lvColors[lvType])
            else 
                levelShowCtrl:SetSelectedIndex( 1 )
            end
        end
  
        if hero_item ~= nil then
            if circleCtrl and circleCtrl.selectedIndex == 1 then
                comIcon.visible = true
                comIcon:GetChild("icon").url = "ui://ICONAtlas/" .. hero_item.f_HeroIcon
            else
                icon.visible = true
                icon:GetChild("icon").url = "ui://ICONAtlas/" .. hero_item.f_HeroIcon
            end
            icon.visible = true
            icon:GetChild("icon").url = "ui://ICONAtlas/" .. hero_item.f_HeroIcon
            if item_info.quality == nil or item_info.quality == 0 then
                item_info.quality = 1
            elseif item_info.quality > #Config.IconQuality then
                item_info.quality = #Config.IconQuality
            end
            quality.visible = true
            qualityBG.visible = true
            quality.url = "ui://ICONAtlas/" .. Config.IconFrame[item_info.quality]
            qualityBG.url = "ui://ICONAtlas/" .. Config.IconQuality[item_info.quality]  
        end 
    end 
end


function UIBattleFail.rendererReward(idx,obj)
    local data = rewards[idx + 1];
    local itemCtr = CtrlManager.GetCtrl(PanelNames.Item);
    itemCtr.initData(obj, data);
end

function UIBattleFail.switchAni()
    Util.AddAction(spineAni, "lose_fidle", true, 0.5);
    --
    --spineAni.AnimationState:SetAnimation(0,"lose_fidle",true);
    --spineAni:Update(0);
end
function UIBattleFail.onClickBGClose()
    TAManager.TrackByDictionary("ui_battle_fail_click_close")
    UIBattleFail.onClickClose()
    if logicMgr.BattlePlaybackManager.GetFightType() == EnumConst.FightTypeConst.FriendAttack then
        --FriendBattleHandler.ReqFriendBattleInfo()
        --FriendBattleHandler.ReqGoodFriendList()
    end
end
function UIBattleFail.onClickClose(closeGuideTrigger)
    if finishFuction ~= nil then
        finishFuction();
    end
    if this.shibaiTime then
        utimer.remove(this.shibaiTime);
    end
    DestoryBattleInUI(1)
    local flag = false;
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON then
        if this.teamNum > 1 then
            UIBattle.onClickBoss();
        else
            if not closeGuideTrigger and FightManager.fightType == EnumConst.FightTypeConst.DUNGEON then
                logicMgr.GuideMgr.AddTrigger(GuideMgr.TriggerType.dungeonFailed, MapManager.getMapId())
                logicMgr.GuideMgr.AddTrigger(GuideMgr.TriggerType.RepeatFailed, MapManager.getMapId())
                flag = true;
            end
        end
    end
    if FightManager.fightType == EnumConst.FightTypeConst.StoryBattle then
            UIBattle.onAutoHardMode(true) 
    end
    if FightManager.fightType == EnumConst.FightTypeConst.Plunder then
        plunderHandler.is_rob = nil 
        if plunderHandler.rob_item_data ~= nil then
            plunderHandler.rob_item_data = nil
        end
        if closeGuideTrigger == nil then
            plunderHandler.sendReqPlunderInfo()
            --showUI(PanelNames.UIBigMap, true)
        end
        if plunderHandler.is_help then
            plunderHandler.is_help = nil
            showUI(PanelNames.UIGuild)
            showUI(PanelNames.UIGuildRescue)
        end
    end
    logicMgr.GuideMgr.Resume()
    --有引导不弹
    if not logicMgr.GuideMgr.IsGuiding() then
        if not closeGuideTrigger and FightManager.fightType == EnumConst.FightTypeConst.DUNGEON then
            if this.teamNum > 1 then

            else
                if logicMgr.DungeonRandomEventManager.getEventState() then
                    logicMgr.DungeonRandomEventManager.OpenDungeonRandomEventUI();
                end
                logicMgr.DungeonRandomEventManager.setEventState(false);
            end
        end
    else
        logicMgr.DungeonRandomEventManager.setEventState(false);
    end
    partnerHandler.tryReqPartnerInteract()
end

function UIBattleFail.getFristHeroData()
    local tempOwner = logicMgr.HeroManager.getOwnerHeroList(false);
    this.tabOwner = {};
    for i=1,#tempOwner do
        if tempOwner[i]["category"] ~= 2 and tempOwner[i]["category"] ~= 3 then
            table.insert(this.tabOwner, tempOwner[i]);
        end
    end
    table.sort(this.tabOwner, logicMgr.HeroManager.sortHero);
    return this.tabOwner[1];
end
function UIBattleFail.onClickHeroRank()
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON and this.teamNum > 1 then
        this.onClickClose();
        return ;
    end
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroRank, true,btnHeroRank) then
        return
    end
    this.onClickClose(true);
    showUI(PanelNames.UICharactorInfoHero, this.getFristHeroData(), 4, nil, function ()
        UIBottom.enterPlayer();
    end);
end

function UIBattleFail.onClickEquipRank()
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON and this.teamNum > 1 then
        this.onClickClose();
        return ;
    end
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroWeapon,true,btnEquipRank) then
        return
    end
    if this.IsFirstClick(true) then
        this.onClickClose()
        return
    end
    this.onClickClose(true);
    showUI(PanelNames.UICharactorInfoHero, this.getFristHeroData(), 5, nil, function ()
        UIBottom.enterPlayer();
    end);
end
---策划要求第一次跳主界面
function UIBattleFail.IsFirstClick(saveFlag)

    if FightManager.fightType == EnumConst.FightTypeConst.Plunder then
        return false
    end
    local flag = GetPlayerPrefs("battleFailedBtnClick")
    if saveFlag then
        SetPlayerPrefs("battleFailedBtnClick","1")
    end
    if flag ~= "1" then
        return true
    end

    return false
end
function UIBattleFail.onClickEquipRefine()
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON and this.teamNum > 1 then
        this.onClickClose();
        return ;
    end
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroEquip,true,btnEquipRefine) then
        return
    end
    if this.IsFirstClick(true) then
        this.onClickClose()
        return
    end
    this.onClickClose(true);
    showUI(PanelNames.UICharactorInfoHero, this.getFristHeroData(), 2, nil, function ()
        UIBottom.enterPlayer();
    end);

end

function UIBattleFail.onClickHeroLevel()
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON and this.teamNum > 1 then
        this.onClickClose();
        return ;
    end
    if this.IsFirstClick(true) then
        this.onClickClose()
        return
    end
    this.onClickClose(true);
    showUI(PanelNames.UICharactorInfoHero, this.getFristHeroData(), 1, nil, function ()
        UIBottom.enterPlayer();
    end);
end

function UIBattleFail.onClickFormation()
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON and this.teamNum > 1 then
        this.onClickClose();
        return ;
    end
    if this.IsFirstClick(true) then
        this.onClickClose()
        return
    end
    this.onClickClose(true);
    UIBattle.onClickBoss();
end

function UIBattleFail.onClickManual()
    --if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON and this.teamNum > 1 then
    --    this.onClickClose();
    --    return ;
    --end
    TAManager.TrackByDictionary("ui_battle_fail_click_manual")
    -- 攻略
    --log('onClickManual');
    if FightManager.fightType == EnumConst.FightTypeConst.DUNGEON then
       -- showUI(PanelNames.UILevelStrategy, FightManager.fightType, this.nextId);
        local formation_type = logicMgr.BattlePlaybackManager.GetFormationType()
        showUI(PanelNames.UIChapterRecord, nil, FightManager.fightType, nil, function ()
            showUI(PanelNames.UIBattleFail, nil, resultData);
        end, nil, formation_type);
    --elseif FightManager.fightType == EnumConst.FightTypeConst.TOWER then
       -- showUI(PanelNames.UIClimbTowerRecord, this.nextId);
    end
end

function UIBattleFail.onClickData()
    -- 查看数据
    --log('onClickData');
    local formation_type = logicMgr.BattlePlaybackManager.GetFormationType() 
    showUI(PanelNames.UILevelRecode, FightManager.fightType, BattlePlaybackManager.getBattleRecord(), nil, function ()
        showUI(PanelNames.UIBattleFail, nil, resultData);
    end, nil, formation_type);
    TAManager.TrackByDictionary("ui_battle_fail_click_data")
end

return UIBattleFail;
